import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wanAndroid/res/colors.dart';

//点击时间
typedef ViewClickListener = void Function();

class UiUts {
  static Widget NoTitlebar() {
    return PreferredSize(
      child: AppBar(
        backgroundColor: ResColors.primaryColor,
      ),
      preferredSize: Size.fromHeight(0),
    );
  }

  static Divider getDivider(
      {double height = 0, Color color = Colors.transparent}) {
    return Divider(
      color: color,
      height: height,
    );
  }

  static Text getText(String data,
      {Color color = ResColors.fontColor,
      double fontSize = 14,
      bool bold = false}) {
    return getTitle(data, color: color, fontSize: fontSize, bold: bold);
  }

  static Text getTitle(String data,
      {Color color = Colors.white, double fontSize = 16, bool bold = true}) {
    return Text(
      "$data",
      softWrap: true,
      style: TextStyle(
        color: color,
        fontSize: fontSize,
        decoration: TextDecoration.none,
        fontWeight: bold ? FontWeight.bold : FontWeight.normal,
      ),
    );
  }

  ///空页面
  static Widget getEmpty({ViewClickListener clickListener}) {
    return Container(
      alignment: Alignment.center,
      width: double.infinity,
      decoration: BoxDecoration(
        color: Colors.white,
      ),
      child: Center(
        child: TextButton(
          onPressed: clickListener,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(
                height: 80,
                width: 80,
                child: Image.asset(
                  "images/icon_no_data.png",
                ),
              ),
              getText(
                "暂无数据···",
                color: Color(0xffbfbfbf),
                fontSize: 20,
              )
            ],
          ),
        ),
      ),
    );
  }

  ///加载界面
  static Widget getLoading({ViewClickListener clickListener}) {
    return Container(
      constraints: BoxConstraints(minHeight: 200, minWidth: double.infinity),
      child: Center(
        child: TextButton(
          onPressed: clickListener,
          child: CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation(ResColors.primaryColor),
          ),
        ),
      ),
    );
  }

  ///Tag
  static Widget getTagView(String name) {
    return Container(
      padding: const EdgeInsets.only(left: 5, right: 5, top: 3, bottom: 3),
      margin: const EdgeInsets.only(right: 5),
      decoration: BoxDecoration(
        color: Color(0xffff824c),
        borderRadius: BorderRadius.circular(3),
      ),
      child: Text(
        '$name',
        style: TextStyle(
          fontSize: 9,
          color: Color(0xffffffff),
        ),
      ),
    );
  }

  /// 显示确认对话框
  static Widget showConfirmDialog(BuildContext buildContext,
      {String title = "温馨提示",
      String message = "是否确认?",
      ViewClickListener clickListener}) {
    return AlertDialog(
      title: getTitle(title, color: Colors.black, fontSize: 17),
      content: getText(message, color: Colors.black, fontSize: 15),
      actions: [
        TextButton(
          onPressed: clickListener,
          child: getText(
            "知道了",
            color: ResColors.primaryColor,
            fontSize: 13,
          ),
        )
      ],
    );
  }

//点击事件
  static Widget onViewClickListener(
      {Widget widget, ViewClickListener clickListener}) {
    return InkWell(
      child: widget,
      onTap: clickListener,
    );
  }

  static void finishRefresh(RefreshController _refreshController) {
    if (null == _refreshController) return;

    ///关闭隐藏
    if (_refreshController.isRefresh) {
      _refreshController.refreshCompleted();
    } else if (_refreshController.isLoading) {
      _refreshController.loadComplete();
    }
  }
}
